package com.baizhi.service.impl;

import com.baizhi.entity.BzAdmin;
import com.baizhi.mapper.BzAdminMapper;
import com.baizhi.service.BzAdminService;
import com.baizhi.service.BzRoleService;
import com.baizhi.service.IBzRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yu
 */
@Service
public class BzAdminServiceImpl implements BzAdminService, UserDetailsService {
    @Autowired
    private BzAdminMapper bzAdminMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private IBzRoleService bzRoleService;
    @Override
    public BzAdmin login(String username, String password) {
        QueryWrapper<BzAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("password",password);
        BzAdmin bzAdmin = bzAdminMapper.selectOne(queryWrapper);
        return bzAdmin;
    }

    @Override
    public Map showAll(Integer pageNum,Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        Page<BzAdmin> bzAdminPage = new Page<>(pageNum,pageSize);
        Page<BzAdmin> page = bzAdminMapper.selectPage(bzAdminPage,null);
        map.put("code",0);
        map.put("data",page.getRecords());
        map.put("count",page.getTotal());
        return map;
    }

    @Override
    public BzAdmin showOne(Integer id) {
        QueryWrapper<BzAdmin> queryWrapper = new QueryWrapper<>();
        BzAdmin bzAdmin = bzAdminMapper.selectById(id);
        return bzAdmin;
    }

    @Override
    public void insert(BzAdmin bzAdmin) {
        bzAdmin.setPassword(bCryptPasswordEncoder.encode(bzAdmin.getPassword()));
        bzAdminMapper.insert(bzAdmin);
    }

    @Override
    public void update(BzAdmin bzAdmin) {
        bzAdminMapper.updateById(bzAdmin);
    }

    @Override
    public void delete(Integer id) {
        QueryWrapper<BzAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        bzAdminMapper.delete(queryWrapper);
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        String code = request.getParameter("code");
        String trueValidateCode = (String) request.getSession().getAttribute("trueValidateCode");
        if(!trueValidateCode.equalsIgnoreCase(code)){
            throw new UsernameNotFoundException("验证码错误");
        }
        BzAdmin bzAdmin = bzAdminMapper.selectOne(new QueryWrapper<BzAdmin>().eq("username", s));
        if(bzAdmin==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        Set<String> rolesByUsername = bzRoleService.getRolesByUsername(s);
        Set<String> permissionsByUsername = bzRoleService.getPermissionsByUsername(s);
        List<GrantedAuthority> collect = rolesByUsername.stream()
                .map(new Function<String, GrantedAuthority>() {
                    @Override
                    public GrantedAuthority apply(String s) {
                        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_" + s);
                        return simpleGrantedAuthority;
                    }
                }).collect(Collectors.toList());
        List<SimpleGrantedAuthority> collect1 = permissionsByUsername.stream()
                .map(new Function<String, SimpleGrantedAuthority>() {
                    @Override
                    public SimpleGrantedAuthority apply(String s) {
                        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(s);
                        return simpleGrantedAuthority;
                    }
                }).collect(Collectors.toList());
        collect.addAll(collect1);
        bzAdmin.setGrantedAuthorities(collect);
        return bzAdmin;
    }
}
